<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
if(check_admin()){
	if(!empty($_REQUEST['equipe'])){
		$id = $_REQUEST['equipe'];
		echo "<table>\n";
		echo "<tr class = 'th'><th>jour</th><th>heures</th><th>salle</th><th>match</th><th>actions</th></tr>\n";
		$q = "select * from entrainements where equipe_id = '$id'";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$salle = new salle($s->salle);
			echo "<tr><td id = 'jour_$s->id'>".($jours[$s->jour])."</td>\n";
			echo "<td id = 'heures_$s->id'>$s->heure</td>\n";
			echo "<td id = 'salle_$s->id'>".(CapitalizeLastName(strtolower($salle->nom)))."</td>\n";
			$match = ($s->match) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
			echo "<td id = 'match_$s->id'>$match</td>\n";
			echo "<td id = 'action_$s->id' class  ='center'><a href = 'javascript: form_edit_entrainement($s->id)'><img src = 'images/icon_edit.png' alt = 'edit' width  '14' height = '14' /></a>";
			echo "<a href = 'javascript: delete_entrainement($s->id); '><img src  ='images/icon_delete.png' alt = 'suppr.' height = '14' width = '14' /></a></td>\n";
			echo "</tr>\n";
		}
		echo "<tr><td id = 'jour_new'></td><td id = 'heures_new'></td><td id = 'salle_new'></td><td id = 'match_new'></td><td id = 'action_new' class = 'center'><a href = 'javascript: form_edit_entrainement(\"new\")'><img src = 'images/icon_new.png' alt = 'nouv.' width = '14' height = '14' /></a></td></tr>\n";
		echo "</table>\n";
	}
	else{
		$variables = array("id","jour","hd","md","hf","mf","salle","match","del");
		foreach($variables as $var) $$var = (!empty($_REQUEST[$var])) ? $_REQUEST[$var] : "";
		if($id){
			if($del){
				$q = "select equipe_id from entrainements where id = $id";
				$r = ExecRequete($q);
				$equipe_id = ($s = LigneSuivante($r)) ? $s->equipe_id : "";
				$q = "delete from entrainements where id = $id limit 1";
				ExecRequete($q);
				echo $equipe_id;
			}
			elseif($hd && $hf && $jour && $salle && $hd < $hf){
				if(!$md) $md = 00;
				if(!$mf) $mf = 00;
				$heure = $hd."h".$md." - ".$hf."h".$mf;
				if($id == 'new'){
					$equipe  = (!empty($_SESSION['equipe'])) ? $_SESSION['equipe'] : "";
					if($equipe){
						$eq = new equipe($equipe,"id_vbcll");
						$q = "insert into entrainements (equipe_id, jour, salle, heure, `match`) values ('$eq->id','$jour','$salle', '$heure','$match')";
						ExecRequete($q);
						echo $eq->id;
					}
				}
				else{
					$q = "update entrainements set jour = '$jour', heure = '$heure', salle = '$salle', `match` = '$match' where id = '$id'";
					ExecRequete($q);
					echo $jours[$jour];
					echo "<!--!>";
					echo $heure;
					echo "<!--!>";
					$obj_salle = new salle($salle);
					echo (CapitalizeLastName(strtolower($obj_salle->nom)));
					echo "<!--!>";
					$print_match = ($match) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
					echo $print_match;
					echo "<!--!>";
					echo "<a href = 'javascript: form_edit_entrainement($id)'><img src = 'images/icon_edit.png' alt = 'edit' width  '14' height = '14' /></a>";
					echo "<a href = 'javascript: delete_entrainement($id);'><img src  ='images/icon_delete.png' alt = 'suppr.' height = '14' width = '14' /></a>";				
				}
			}
			else{
				if($id == 'new'){
					echo "<select id = 'jour'>";
					foreach($jours as $abr => $nom){
						echo "<option value = '$abr'>$nom</option>";
					}
					echo "</select><!--!>";
					echo "<input type =  'text' size = '2' maxlength = '2' value = '00' id = 'hd' /> h <input type =  'text' size = '2' maxlength = '2' value = '00' id = 'md' />";
					echo "-<input type =  'text' size = '2' maxlength = '2' value = '00' id = 'hf' /> h <input type =  'text' size = '2' maxlength = '2' value = '00' id = 'mf' />";
					echo "<!--!>";
					$salles = array(610 => "Les P&acirc;les", 620 => "Corsy", 611 => "La Croix", 600 => "Chamblandes 1", 601 => "Chamblandes 2");
					echo "<select id = 'salle'>";
					foreach($salles as $no => $salle){
						echo "<option value = '$no'>$salle</option>";
					}
					echo "</select>";
					echo "<!--!>";
					echo "<input type = 'checkbox' name = 'match' id = 'match' value = '1' />";
					echo "<!--!>";
					echo "<button type = 'button' onclick = \"var match = (document.getElementById('match').checked) ? 1 : 0; edit_entrainement('$id',document.getElementById('jour').value, document.getElementById('hd').value, document.getElementById('md').value, document.getElementById('hf').value, document.getElementById('mf').value, document.getElementById('salle').value,match)\">modifier</button>";
				}
				else{
					$q = "select * from entrainements where id = $id";
					$r = ExecRequete($q);
					if($s = LigneSuivante($r)){
						echo "<select id = 'jour'>";
						foreach($jours as $abr => $nom){
							$selected = ($abr == $s->jour) ? "selected = 'selected'" : "";
							echo "<option value = '$abr' $selected>$nom</option>";
						}
						echo "</select><!--!>";
						ereg("([0-9]{1,2})h([0-9]{1,2})\ *-\ *([0-9]{1,2})h([0-9]{1,2})",$s->heure,$regs);
						echo "<input type =  'text' size = '2' maxlength = '2' value = '$regs[1]' id = 'hd' /> h <input type =  'text' size = '2' maxlength = '2' value = '$regs[2]' id = 'md' />";
						echo "-<input type =  'text' size = '2' maxlength = '2' value = '$regs[3]' id = 'hf' /> h <input type =  'text' size = '2' maxlength = '2' value = '$regs[4]' id = 'mf' />";
						echo "<!--!>";
						$salles = array(610 => "Les P&acirc;les", 620 => "Corsy", 611 => "La Croix", 600 => "Chamblandes 1", 601 => "Chamblandes 2");
						echo "<select id = 'salle'>";
						foreach($salles as $no => $salle){
							$selected = ($s->salle == $no) ? "selected = 'selected'" : "";
							echo "<option value = '$no' $selected>$salle</option>";
						}
						echo "</select>";
						echo "<!--!>";
						$checked = ($s->match) ? "checked = 'checked'" : "";
						echo "<input type = 'checkbox' name = 'match' id = 'match' value = '1' $checked />";
						echo "<!--!>"; 
						echo "<button type = 'button' onclick = \"var match = (document.getElementById('match').checked) ? 1 : 0; edit_entrainement('$id',document.getElementById('jour').value, document.getElementById('hd').value, document.getElementById('md').value, document.getElementById('hf').value, document.getElementById('mf').value, document.getElementById('salle').value,match)\">modifier</button>";
					}							
				}
			}
		}		
	}
}
else error_log("NOT admin");
?>
